
// 选中评论
function commentItemClick (target) {
    if (target.parentNode.parentNode.getAttribute('comment-item-clicked') === 'true'){
        target.parentNode.parentNode.setAttribute('comment-item-clicked', false);
        $(target.parentNode.parentNode).removeClass('comment-item-clicked')
    } else {
        target.parentNode.parentNode.setAttribute('comment-item-clicked', true);
        $(target.parentNode.parentNode).addClass('comment-item-clicked')
    }
}

// 下载评论
async function downloadComments() {
    if (document.documentElement.getAttribute('lang') === 'zh-CN') {
        alert('当前语言为中文')
        return
    }
    const selectedItems = $('.comment-item-clicked')
    const downBtn = $('#download-comment')
    // downBtn.disable()
    await new Promise((resolve => {
        setTimeout(()=> {
            downBtn.attr("disabled", true);
            downBtn.text('准备下载')
            resolve()
        }, 200)
    }))

    if (selectedItems != null) {
        if (selectedItems.length == 0) {
            downBtn.text('下载评论')
            downBtn.attr("disabled",false);
            alert('请选择要下载的评论')
            return
        }

        const canvas = document.getElementById('comment-item-render')
        const scale = window.devicePixelRatio; //定义任意放大倍数 支持小数
        // 去图片锯齿 官网
        const context = canvas.getContext('2d');
        context.mozImageSmoothingEnabled = false;
        context.webkitImageSmoothingEnabled = false;
        context.msImageSmoothingEnabled = false;
        context.imageSmoothingEnabled = false;
        const zip = new JSZip();
        let cnComment = `视频地址：${location.href}\n`
        let currentNode = null;
        for (let i = 0; i < selectedItems.length; i++) {
            downBtn.text(`正在解析第${(i + 1)}/${selectedItems.length}个评论...`)
            currentNode = selectedItems[i];
            const bodyElement = currentNode.body
            const width = bodyElement.offsetWidth; //获取dom 宽度
            const height = bodyElement.offsetHeight; //获取dom 高度
            // 截图
            if (bodyElement) {
                canvas.width = width * scale; //定义canvas 宽度 * 缩放
                canvas.height = height * scale; //定义canvas高度 *缩放
                // options配置
                const opts = {
                    scale: scale,
                    canvas: canvas,
                    logging: false,
                    width: width,
                    height: height,
                    dpi: 300,
                    useCORS: true,
                    backgroundColor: "transparent",
                    allowTaint: true,
                };
                await html2canvas(bodyElement, opts)
                const blob = await new Promise((resolve => {
                    canvas.toBlob((blob => resolve(blob)))
                }))
                if (blob) {
                    zip.file(`${i}.png`, blob);
                }
            }
            // 评论文本
            const textElement = currentNode.$['content-text']
            if (textElement != null) {
                let text = textElement.textContent
                let cnText = await GoogleTranslate.single(text)
                cnComment += `${i}====>>>\n${text}\n`
                cnComment += `${i}====>>>cn\n${cnText ? cnText : ''}\n`
            }
            console.log(`第${i}个评论完成`)
        }
        console.log(`添加中文评论:` + JSON.stringify(cnComment, null, 2))
        zip.file("中文评论.txt", cnComment);
        console.log(`添加中文评论完成`)
        zip.file("dl-1080.sh", `#!/bin/bash\nyoutube-dl '${location.href}' -f 'bestvideo[height<=1080]+bestaudio/best[height<=1080]'`)
        zip.generateAsync({
            type:"blob",
            compression: "DEFLATE",
            compressionOptions: {
                /* compression level ranges from 1 (best speed) to 9 (best compression) */
                level: 1
            }
        }).then(function(content) {
                // see FileSaver.js
                console.log('准备保存')
                downBtn.text('下载评论')
                downBtn.attr("disabled",false);
                saveAs(content, `${document.title}.zip`);
                // downBtn.enable()
            });
    }
}

function googleTranslateElementInit() {

    new google.translate.TranslateElement(
        {
            //这个参数不起作用，看文章底部更新，翻译面板的语言
            //pageLanguage: 'zh-CN',
            //这个是你需要翻译的语言，比如你只需要翻译成越南和英语，这里就只写en,vi
            includedLanguages: 'en,zh-CN,hr,cs,da,nl,fr,de,el,iw,hu,ga,it,ja,ko,pt,ro,ru,sr,es,th,vi',
            //选择语言的样式，这个是面板，还有下拉框的样式，具体的记不到了，找不到api~~
            layout: google.translate.TranslateElement.InlineLayout.SIMPLE,
            //自动显示翻译横幅，就是翻译后顶部出现的那个，有点丑，这个属性没有用的话，请看文章底部的其他方法
            autoDisplay: true,
            //还有些其他参数，由于原插件不再维护，找不到详细api了，将就了，实在不行直接上dom操作
        },
        'google_translate_element'//触发按钮的id
    );
}
